package cn.edu.bs.web.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bs
 */
@Controller
public class AppErrorController extends AbstractErrorController {

    private final static String ERROR_PATH = "/error";

    @Autowired
    public AppErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }


    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(value = ERROR_PATH, produces = "text/html")
    public ModelAndView errorHtml(HttpServletRequest request) {
        Map<String, Object> map = this.getErrorAttributes(request, false);
        ModelAndView mav = null;

        HttpStatus httpStatus = this.getStatus(request);
        Integer status = httpStatus.value();
        if (status == 404) {
            mav = new ModelAndView("error/404", map);
        } else if (status == 403) {
            mav = new ModelAndView("error/403", map);
        } else if (status == 500) {
            mav = new ModelAndView("error/500", map);
        }
        return mav;
    }

}
